package com.ctgu.examonline.controller;

import com.ctgu.examonline.entity.Major;
import com.ctgu.examonline.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by 段敏杰 at 2020-08-06 13:22
 */
@RestController
@RequestMapping("/major")
public class MajorController {
    @Autowired
    private MajorService majorService;

    // 查询所有专业
    @RequestMapping("/list")
    public List<Major> majorList() {
        return majorService.getMajorList();
    }

    // 添加一个专业
    @RequestMapping("/save")
    public boolean saveOneMajor(Major major) {
        try {
            majorService.saveOneMajor(new Major(null, "西班牙语"));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    // 删除一个专业
    @RequestMapping("/delete")
    public boolean delete(String name) {
        // TODO: 2020/8/6 0006 没有"日语22"专业也返回true
        try {
            majorService.deleteOneMajor("日语22");
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    // 修改一个专业
    @RequestMapping("/update")
    public boolean update(String newName, String oldName) {
        // TODO: 2020/8/6 0006 bug:如果数据库不存在newName,仍然返回true
        try {
            majorService.updateOneMajor("金融", "印度语");
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
